package net.chenlin.dp.modules.business.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.modules.business.dao.DistrictManageMapper;
import net.chenlin.dp.modules.business.dao.TcpDataMapper;
import net.chenlin.dp.modules.business.entity.vo.districtManage.DistrictTreeVo;
import net.chenlin.dp.modules.business.service.TreeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yangsm
 * @ClassName: TreeServiceImpl
 * @Description: TODO
 * @date 2023年06月12日 11:21
 */
@Service
@Slf4j
public class TreeServiceImpl implements TreeService {

    @Autowired
    private DistrictManageMapper districtManageMapper;

    @Autowired
    private TcpDataMapper tcpDataMapper;

    @Override
    public List<String> getAllChildrenIdWithParentId(String treeId, Integer districtId) {
        List<DistrictTreeVo> trees = districtManageMapper.getDeviceById(Long.valueOf(districtId));
        List<DistrictTreeVo> childTrees = new ArrayList<>();
        // 递归
        this.treeOrgChildren(childTrees, trees, treeId);
        List<String> childList = new ArrayList<>();
        for (DistrictTreeVo treeDTO : childTrees) {
            childList.add(treeDTO.getDeviceNo());
        }
        childList.add(treeId);
        return childList;
    }

    /**
     * 获取指定菜单的所有子节点
     *
     * @param childTrees   子节点收集器
     * @param trees        当前菜单
     * @param parentTreeId 父菜单ID
     */
    private void treeOrgChildren(List<DistrictTreeVo> childTrees, List<DistrictTreeVo> trees, String parentTreeId) {
        for (DistrictTreeVo tree : trees) {
            if (tree.getParentId() != null && tree.getParentId().equals(parentTreeId)) {
                treeOrgChildren(childTrees, trees, tree.getDeviceNo());
                childTrees.add(tree);
            }
        }
    }

    @Override
    public Map<String,Double> toParentMap(Map<String,Double> map, String paramDeviceNo) {
        if(map.size() == 1 && map.get(paramDeviceNo) != null){
            return map;
        }
        Map<String,Double> parentMap = new HashMap<>();

        Double onEleTotal = 0.00;
        for(String key: map.keySet()){
            String parentDeviceNo = tcpDataMapper.getParentDeviveNo(key);
            if(map.get(parentDeviceNo) == null){
                map.put(parentDeviceNo,onEleTotal);
            }
            if(StringUtils.isNotEmpty(parentDeviceNo)){
                Double aDouble = map.get(key);
                Double aDouble1 = parentMap.get(parentDeviceNo);
                BigDecimal b1 = new BigDecimal(Double.toString(aDouble1));
                BigDecimal b2 = new BigDecimal(Double.toString(aDouble));
                double v = b1.add(b2).doubleValue();
                map.put(parentDeviceNo,v);
            }
        }
        if(parentMap.size() > 1 ){
            toParentMap(parentMap,paramDeviceNo);
        }
        return parentMap;
    }
}
